package com.ayoka.parenting.adapter;

import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.widget.ImageView;

import com.ayoka.parenting.R;
import com.ayoka.parenting.bean.CommentBean;
import com.ayoka.parenting.bean.UserManager;
import com.ayoka.parenting.widget.CommentListView;
import com.jincp.common.brvah.BaseQuickAdapter;
import com.jincp.common.brvah.BaseViewHolder;
import com.jincp.common.utils.DateUtil;
import com.jincp.common.utils.UIUtils;
import com.jincp.image.load.ImageLoader;
import com.jincp.image.load.ImageLoaderOptions;

import java.util.Date;
import java.util.List;

/**
 * Created by jinchunping on 2018/4/11.
 */

public class TopicCommentAdapter extends BaseQuickAdapter<CommentBean, BaseViewHolder> {

    // topic发表者的Id
    private String topicOwnerId;

    public TopicCommentAdapter(String topicOwnerId) {
        super(R.layout.item_topic_comment);
        this.topicOwnerId = topicOwnerId;
    }

    @Override
    protected void convert(final BaseViewHolder holder, final CommentBean item) {
        // 设置案主信息
        if (item.UserInfo != null) {
            ImageView ivHeader = holder.getView(R.id.iv_header);
            ImageLoader.getInstance().displayImage(item.UserInfo.LocalAvatar, ivHeader, ImageLoaderOptions.getCircleOptions());
            holder.setText(R.id.tv_name, item.UserInfo.WeixinNickName);
        }

        // 设置文本消息
        holder.setText(R.id.tv_content, getCommentContent(item))
                .setVisible(R.id.tv_content, !TextUtils.isEmpty(item.Content));

        // 设置评论列表
        CommentListView commentList = holder.getView(R.id.commentList);
//        if (item.Comments == null || item.Comments.isEmpty()) {
//            // 设置评论layout不显示
//            holder.setVisible(R.id.ll_comment_container, false);
//        } else {
//            holder.setVisible(R.id.ll_comment_container, true);
//            commentList.setDatas(item.Comments, false);
//        }

        // 设置时间
        Date publishDate = DateUtil.parseDate(item.CreateDate);
        holder.setText(R.id.tv_date, DateUtil.formatDate(publishDate, "yyyy年MM月dd日"))
                .setText(R.id.tv_praise, String.valueOf(item.LikeCount))
                .setSelected(R.id.tv_praise, item.IsLike == 1)
                .setSelected(R.id.tv_star, item.IsGood == 1)
                .setVisible(R.id.tv_star, getStarVisible(item.Uid))
                .addOnClickListener(R.id.tv_comment)
                .addOnClickListener(R.id.tv_star)
                .addOnClickListener(R.id.tv_praise);
    }

    private SpannableStringBuilder getCommentContent(CommentBean item) {
        SpannableStringBuilder builder = new SpannableStringBuilder();
        if (item.ReplyToUserInfo != null) {
            String replyToUserName = item.ReplyToUserInfo.WeixinNickName;
            builder.append("回复 ");
            int startIndex = builder.length();
            builder.append(replyToUserName);
            int endIndex = builder.length();
            builder.setSpan(new ForegroundColorSpan(UIUtils.getColor(R.color.blue_300)), startIndex, endIndex, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            builder.append("：");
            builder.append(item.Content);
            return builder;
        }
        builder.append(item.Content);
        return builder;
    }

    public boolean getStarVisible(String commentOwnerId){
        // 如果用户不是圈主 和 topic的发表人  则没有权限
        // 用户的Id
        String userId = UserManager.getInstance().uid;
        if (!TextUtils.equals(userId, commentOwnerId) && !TextUtils.equals(userId, topicOwnerId)) {
            return false;
        }
        return true;
    }
}
